package jwm.data;

import java.net.MalformedURLException;
import java.net.URL;

import jwm.Main;

public class AElement {

   private boolean valid = false;
   private URL url;
   private URL parentURL;
   private String[] suffixes = {"html", "htm", "php", "txt", "xhtml", "phtml", "php4", "php5", "asp", "aspx", "jsp", "jspx", "py"};
   private String[] nonCrawlSuffixes = {"pdf", "swf"};
   private boolean isPDF = false;
   
   public AElement(String element, URL parentURL) {
      
      this.parentURL = parentURL;
      element = element.replace('\'', '"').toLowerCase();
      /*************************
       *     NOFOLLOW LINK     *
       *************************/
      if (element.contains("rel=\"nofollow\"")) {
         return;
      }
      setURL(element);
   }
   
   private void setURL(String element) {
      /*************************
       *     CONTAINS href     *
       *************************/
      String href = "href=\"";
      int index = element.indexOf(href);
      if (index != -1) {
         element = element.substring(index + href.length());
      } else {
         //System.out.println("DOES NOT CONTAIN HREF");
         return;
      }
      
      /*************************
       *  BEGINS WITH HTTP(S)  *
       *************************/
      if (!(element.startsWith("http://") || element.startsWith("https://"))) {
         element = Main.url + element;
      }
      
      /*************************
       *   DOES NOT CONTAIN "  *
       *************************/
      index = element.indexOf('"');
      if (index != -1) {
         element = element.substring(0, index);
         element = element.substring(0, 7) + element.substring(7).replace("//", "/");
      } else {
         //System.out.println("CONTAINS \"");
         return;
      }
      
      /*************************
       *       VALID URL       *
       *************************/
      try {
         url = new URL(element);
      } catch (MalformedURLException e) {
         System.out.println("INVALID URL");
         return;
      }
      
      /*************************
       *  SAME SITE AS PARENT  *
       *************************/
      String authority = url.getAuthority();
      if (authority.split("\\.").length < 3) authority = "www.".concat(authority);
      String parentAuthority = parentURL.getAuthority();
      if (parentAuthority.split("\\.").length < 3) parentAuthority = "www.".concat(parentAuthority);
      if (!authority.equals(parentAuthority)) {
         //System.out.println("NOT SAME AS PARENT");
         return;
      }

      /*************************
       *     IS TEXT FILE      *
       *************************/
      String[] urlParts = url.getFile().split("\\.");
      if (urlParts.length > 1) {
         boolean contains = false;
         for (String suffix : nonCrawlSuffixes) {
            if (urlParts[urlParts.length - 1].startsWith(suffix)) {
               isPDF = true;
               break;
            }
         }
         for (String suffix : suffixes) {
            if (urlParts[urlParts.length - 1].startsWith(suffix)) {
               contains = true;
               break;
            }
         }
         if (!contains) {
            //System.out.println("IS NOT TEXT FILE");
            return;
         }
      }
      valid = true;
   }

   public boolean isValid() {
      return valid;
   }

   public URL getUrl() {
      return url;
   }

   public boolean isPDF() {
      return isPDF;
   }
   
}
